home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8350 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  78 lines

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 29 Feb 96 14:47:25 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.825605245@rscernix>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <4gsdno$1bg@umbc9.umbc.edu> <4gtab6$acb@ceylon.gte.com> <313318b8.53776146@nntp.ix.netcom.com> <4h1u9d$sqq@ceylon.gte.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4h1u9d$sqq@ceylon.gte.com> Brenda <g051286> writes:
  13.  
  14. >What is the definition of a pointer?  I have always been taught that a
  15. >pointer is simply an address in memory, and an array name (i.e. myarray)
  16. >is simply a CONSTANT address.
  17.  
  18. Your teacher obviously didn't know what s/he was talking about.
  19. Or you grossly misunderstood what s/he taught you.  And you definitely
  20. didn't bother reading the FAQ before posting.
  21.  
  22. > I don't think this statement is all that
  23. >radical.  Of course there are differences between arrays and pointers
  24. >due to the fact that an array is a CONSTANT address and a pointer is a
  25. >VARIABLE address.
  26.  
  27. There are also differences caused by the fact that arrays are NOT pointers.
  28. For the benefit of people who need to see to be able to believe, here's
  29. a simple example:
  30.  
  31.     cnwgs1:~/tmp 13> cat dumb.c
  32.     #include <stdio.h>
  33.     char array[] = "abcd";
  34.  
  35.     foo()
  36.     {
  37.     printf("dumb.c\narray is stored at address: %p\n", (void *)array);
  38.     printf("array contains: %s\n", array);
  39.     }
  40.  
  41.     cnwgs1:~/tmp 14> cat dumber.c
  42.     #include <stdio.h>
  43.     extern char *array;  /* arrays are pointers, right? :-) */
  44.  
  45.     main()
  46.     {
  47.     foo();
  48.     puts("dumber.c");
  49.     printf("array is pointing at the address: %p\n", (void *)array);
  50.     printf("%x='a' %x='b' %x='c' %x='d'\n", 'a', 'b', 'c', 'd'); 
  51.     printf("array contains: %s\n", array);
  52.     return 0;  /* Not reached */
  53.     }
  54.  
  55.     cnwgs1:~/tmp 15> cc dumb.c dumber.c
  56.     dumb.c:
  57.     dumber.c:
  58.     cnwgs1:~/tmp 16> ./a.out
  59.     dumb.c
  60.     array is stored at address: 200524b8
  61.     array contains: abcd
  62.     dumber.c
  63.     array is pointing at the address: 61626364
  64.     61='a' 62='b' 63='c' 64='d'
  65.     Segmentation fault (core dumped)
  66.  
  67. Looks like there is something wrong with the "arrays are pointers" theory.
  68. I've provided all the clues in the output, so that anybody can figure out
  69. what actually happens in this program.  The FAQ explains this with a nice
  70. picture, but since some people cannot be convinced to read it...
  71.  
  72. Dan
  73. --
  74. Dan Pop
  75. CERN, CN Division
  76. Email: danpop@mail.cern.ch 
  77. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  78.